package com.frxuexi.shop2307.user

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.databinding.ActivityUpdateInfoBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/UpdateInfoActivity")
class UpdateInfoActivity : BaseActivity<ActivityUpdateInfoBinding,UserViewModel>() {
    var path=""

    override fun initData() {
        vm.infoData.observe(this){
            binding.infoName.setText(it.nickname)
            binding.infoPhone.setText(it.phone)
            GlideUtils.getCircleImage(binding.infoIv,it.avatar)
        }
        vm.updateInfoData.observe(this){
            ToastUtils.showLong("修改成功")
            finish()
        }
    }

    override fun initView() {
        binding.infoOut.setOnClickListener {
            SPUtils.getInstance().apply {
                remove(Const.TOKEN)
                remove(Const.PHONE)
            }
            ARouter.getInstance().build("/app/LoginActivity").navigation()

        }

        binding.infoPass.setOnClickListener{
            ARouter.getInstance().build("/app/FindPassActivity").navigation()
        }

        binding.infoIv.setOnClickListener {
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        vm.getInfo()

        binding.infoSave.setOnClickListener {
            vm.updateInfo(mapOf("avatar" to path,"nickname" to binding.infoName.text.toString()))
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            GlideUtils.getCircleImage(binding.infoIv,bean?.path!!)
            path=bean.path
        }
    }
}